<?php
class GoodsController extends AdminBase{

	public function actionIndex(){
        $objModel = new GoodsModel('admin');
        $objModel->unsetAttributes();

        if(isset($_GET['GoodsModel']) || isset($_GET['key']))
        {
        	if(!empty($_GET['key'])){
        		switch ($_GET['key']) {
        			case '1':
        				$objModel->ProductID = (double)$_GET['keyword'];
        				break;
        			case '2':
        				$objModel->GoodsName = $_GET['keyword'];
        				break;
        			case '3':
        				$objModel->Nick = $_GET['keyword'];
        				break;
        			case '4':
        				$objModel->GoodsBmID = $_GET['keyword'];
        				break;       			
        			default:
        				# code...
        				break;
        		}
        	}
            $objModel->attributes = $_GET['GoodsModel'];
        }
        //$objModel->IsIndex == 0 ? $objModel->IsIndex = '' : '';
        $objCriteria = $objModel->createSearchCriteria();
        
        //分页
        $pageId = isset($_GET['page']) ? (int) $_GET['page'] : 1;
        //信息总数
        $count = $objModel->count($objCriteria);
        //一页显示10条
        $limit = 20;
        //分页
        $page = self::page($count, $limit, $pageId,'',$objCriteria);

        //排序
        
        $objSort = new CSort('GoodsModel');
        $objSort->defaultOrder = 'GoodsID desc,Sorting DESC';        
        $objSort->applyOrder($objCriteria);


        $data = $objModel->findAll($objCriteria);
        $this->assign($_GET);
        $this->assign("Page", $page->show());
        $this->assign('data', $data);
        $this->assign('sort', $objSort);
        $this->assign('model', $objModel);
        $this->assign('count', $count);   
        $this->render('index');      
	}

    //取消活动
    //就是把用户的这个商品信息删除，然后把报名的状态改为已取消的状态
    public function actionActivity() {
        $objModel = $this->loadModel(Yii::app()->request->getParam('id'));
        if ($objModel->activity() !== false) {
            $content = $this->renderPartial('data', array('rs' => $objModel), true);
            $this->ajaxReturn(array('status' => true,'content' => $content));
        } else {
            $error = $model->getOneError();
            $this->error(yii::app()->user->getFlash());
        }
    }

    //添加商品
    public function actionCreate(){
        $objModel = new GoodsModel('admin');
        $objModel->resetAttributes();
        $objModel->Status = 1;
        $objModel->IsFreeShipping = 1;
        $objModel->StartTime = strtotime('today 9:00');
        $objModel->EndTime = strtotime('+3day 9:00');
        if(isset($_POST['GoodsModel'])){
            //赋值
            $objModel->attributes = $_POST['GoodsModel'];
            if($objModel->save()){ 
                //添加成功了后然后添加标签
                                          
                $this->success('添加成功',$this->createUrl('index'));     
            }else{
                $error = $objModel->getOneError();
                $this->error($error ? $error : '添加失败');         
            }
        }

        $this->render('create', array('model' => $objModel));   
    }


	//修改商品
	public function actionUpdate($id){
        $this->baseUpdate('GoodsModel');   
	}

    //单个的修改商品//是否因为权限问题可合过去。
    public function actionUpdateOne(){
        $model = $this->loadModel(Yii::app()->request->getParam('id'));
        $_POST['GoodsModel']['Tag'] = $model->getTag();
        $_POST['GoodsModel']['Module'] = $model->getModule();
        $this->baseUpdate('GoodsModel', '', true);
    }


	//下架商品
	public function actionDelete($id){
        $model = $this->loadModel($id);
        $model->Status = 5;
        $this->baseUpdate($model, '', true);
	}
    //上架商品
    public function actionShangjia($id){
        $model = $this->loadModel($id);
        $model->Status = 1;
        $this->baseUpdate($model, '', true);
    }
	//显示具体信息
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	//加载model
	public function loadModel($id)
	{
		$model=GoodsModel::model()->findByPk($id);
		if($model===null)
			$this->error('找不到记录');
		return $model;
	}

    public function actionQuickUpdate(){
        $this->baseUpdate('GoodsModel', '', true);
        //$this->baseUpdate('GoodsModel');
    }
    /**
     * 模块
     * @return [type] [description]
     */
    public function actionModuleTag(){
        //获取所有的模块，所有的标签，所有的商品
        
        //首页显示的模块
        if(!empty($_GET['ModuleID'])){
            $modeultOne = ModuleModel::getOne($_GET['ModuleID']);
            $tags = $modeultOne['TagIDs'];
            $model = ModuleModel::model()->findByPk($modeultOne['ModuleID']);            
        }else{
            $model = new ModuleModel('search');
            $tags = ModuleModel::getListTag();
            $tags = BaseModel::getStrIn($tags);  
        }

        //这个模块下面的标签
        $datas = TagModel::model()->findAll(BaseModel::getC(array('TagID' => array('sign' => 'in', 'data' => $tags))));

        $this->assign('count', count($datas));
        $this->assign('model', $model);
        $this->assign('data', $datas);
        $this->render();
    }


}


?>